package com.ibeacon.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibeacon.model.BeaconInfo;
import com.ibeacon.service.IbeaconDeviceService;

import net.sf.json.JSONObject;

@Controller
public class IbeaconGroupController {
	
	@Autowired
	IbeaconDeviceService ibeaconDeviceService;
	
	
	@RequestMapping("/queryIbeaconGroup")
	public String QueryIbeaconGroup(ModelMap model ,HttpServletResponse response,HttpServletRequest request){
		
		JSONObject json	=  	ibeaconDeviceService.queryIbeaconGroup();
		JSONObject data = (JSONObject) json.get("data");
		List<Object> list=  (List<Object>) data.get("groups");
		int total = list.size();
		model.addAttribute("total", total);
		model.addAttribute("json", json);
		return "managegroup/queryIbeaconGroup";
		
	}
	
	@RequestMapping("/addIbeaconGroup")
	public String AddIbeaconGroup(ModelMap model ,HttpServletResponse response,HttpServletRequest request){
		
		 String groupName = request.getParameter("group_name");
		if (groupName !=null &&!"".equals(groupName)){
		  JSONObject json	=  ibeaconDeviceService.addIbeaconGroup(groupName);
		  model.addAttribute("json", json);
		}
		 
		 return "managegroup/addDeviceGroup";
		
	}
	
	@RequestMapping("/deleteIbeaconGroup")
	@ResponseBody
	public JSONObject deleteIbeaconGroup(ModelMap model ,HttpServletResponse response,HttpServletRequest request){

		 String groupId = request.getParameter("group_id");
		 JSONObject json = new JSONObject();
		if (groupId !=null &&!"".equals(groupId)){
		   json	=  ibeaconDeviceService.deleteIbeaconGroup(groupId);
		}
		return json;
	}
	
	@RequestMapping("/deleteIbeaconGroups")
	@ResponseBody
	public String deleteIbeaconGroups(ModelMap model ,HttpServletResponse response,HttpServletRequest request){

		 String groupIds = request.getParameter("groupIds");
		 String retMessage = "";
		if (groupIds !=null &&!"".equals(groupIds)){
			retMessage	=  ibeaconDeviceService.deleteIbeaconGroups(groupIds);
		}
		return retMessage;
	}
	
	
	@RequestMapping("/groupDeviceDetil")
	public String groupDeviceDetil(ModelMap model ,HttpServletResponse response,HttpServletRequest request){
		JSONObject json	=  	ibeaconDeviceService.queryIbeaconGroup();
		JSONObject data = (JSONObject) json.get("data");
		List<Object> list=  (List<Object>) data.get("groups");
		int total = list.size();
		model.addAttribute("total", total);
		model.addAttribute("json", json);
		return "managegroup/groupDeviceDetil";
		
	}
	
	@RequestMapping("/queryIbeaconGroupDetil")
	public String QueryGroupDetil(ModelMap model ,HttpServletResponse response,HttpServletRequest request){
		
		 String groupId = request.getParameter("group_id");
		 JSONObject json	= new JSONObject();
		if (groupId !=null &&!"".equals(groupId)){
		  json	=  ibeaconDeviceService.queryGroupDetil(groupId);
		  model.addAttribute("json", json);
		}
		JSONObject data = (JSONObject) json.get("data");
		List<Object> list=  (List<Object>) data.get("devices");
		int total = list.size();
		model.addAttribute("total", total);
		return "managegroup/queryIbeaconGroupDetil";
		
	}
	
	
	@RequestMapping("/editIbeaconGroup")
	public String editIbeaconGroup(ModelMap model ,HttpServletResponse response,HttpServletRequest request){
		
		 String groupId = request.getParameter("group_id");
		 String groupName = request.getParameter("group_name");
		if (groupName !=null &&!"".equals(groupName)){
		  JSONObject json	=  ibeaconDeviceService.editGroupDetil(groupId,groupName);
		  model.addAttribute("json", json);
		}
		return "managegroup/editDeviceGroup";
		
	}
	
	@RequestMapping("/queryDevice")
	public String queryDevice(ModelMap model ,HttpServletResponse response,HttpServletRequest request){
		
		
	
		 List<BeaconInfo>	list=  ibeaconDeviceService.queryDevice();
		  model.addAttribute("list", list);
		int total = list.size();
		model.addAttribute("total", total);
		return "managegroup/queryDevice";
		
	}

}
